package com.quick.controller.system;
import java.util.List;

import com.quick.common.core.domain.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.quick.common.annotation.Log;
import com.quick.common.core.controller.BaseController;
import com.quick.common.core.domain.ApiResponse;
import com.quick.common.enums.BusinessTypeEnum;
import com.quick.system.domain.SysDictType;
import com.quick.system.service.SysDictTypeService;
import com.quick.common.utils.poi.ExcelUtil;
/**
 * 字典类型Controller
 *
 * @Author: 吃素不吃肉
 * @Date: Tue Jul 25 14:14:44 CST 2023
 */
@Api(value = "字典类型管理", tags = {"字典类型操作接口"})
@RestController
@RequestMapping("/system/dictType")
public class SysDictTypeController extends BaseController {
    @Autowired
    private SysDictTypeService sysDictTypeService;

   /**
    * 查询字典类型列表
    */
    @PreAuthorize("@ps.hasPermission('system:dict:list')" )
    @GetMapping("/list")
    @ApiOperation(value = "分页查询字典类型列表")
    public ApiResponse list(SysDictType sysDictType) {
        try {
            startPage();
            List<SysDictType> list = sysDictTypeService.selectSysDictTypeList(sysDictType);
            return paging(list);
        } finally {
            clearPage();
        }

    }

    /**
     * 导出字典类型列表
     */
    @PreAuthorize("@ps.hasPermission('system:dict:export')" )
    @Log(title = "字典类型", businessType = BusinessTypeEnum.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出字典类型列表")
    public void export(HttpServletResponse response, SysDictType sysDictType) {
        List<SysDictType> list = sysDictTypeService.selectSysDictTypeList(sysDictType);
        ExcelUtil<SysDictType> util = new ExcelUtil<>(SysDictType.class);
        util.exportExcel(response, list, "字典类型数据");
    }

    /**
     * 获取字典类型详细信息
     */
    @PreAuthorize("@ps.hasPermission('system:dict:query')")
    @GetMapping(value = "/{dictId}")
    @ApiOperation(value = "获取字典类型详细信息")
    public ApiResponse getInfo(@PathVariable("dictId") Long dictId) {
        return success(sysDictTypeService.selectSysDictTypeByDictId(dictId));
    }

    /**
     * 新增字典类型
     */
    @PreAuthorize("@ps.hasPermission('system:dict:add')")
    @Log(title = "字典类型", businessType = BusinessTypeEnum.INSERT)
    @PostMapping
    @ApiOperation(value = "新增字典类型")
    public ApiResponse add(@RequestBody SysDictType sysDictType) {
        return success(sysDictTypeService.insertSysDictType(sysDictType));
    }

    /**
     * 修改字典类型
     */
    @PreAuthorize("@ps.hasPermission('system:dict:edit')")
    @Log(title = "字典类型", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改字典类型")
    public ApiResponse edit(@RequestBody SysDictType sysDictType) {
        return success(sysDictTypeService.updateSysDictType(sysDictType));
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize("@ps.hasPermission('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/{dictIds}")
    @ApiOperation(value = "删除字典类型")
    public ApiResponse remove(@PathVariable Long[] dictIds) {
        return success(sysDictTypeService.deleteSysDictTypeByDictIds(dictIds));
    }

    /**
     * 查询字典数据信息
     */
    @PreAuthorize("@ps.hasPermission('system:dict:list')")
    @GetMapping(value = "/option")
    @ApiOperation(value = "查询字典数据信息")
    public ApiResponse option() {
        List<Dict> data = sysDictTypeService.selectSysDictTypeList();
        return success(data);
    }
    /**
     * 刷新字典缓存
     */
    @PreAuthorize("@ps.hasPermission('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessTypeEnum.CLEAN)
    @DeleteMapping("/refreshCache")
    public ApiResponse refreshCache()
    {
        sysDictTypeService.resetDictCache();
        return success();
    }
}
